﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShoppingApp
{
    public partial class ViewStateExample : System.Web.UI.Page
    {
        private string storedTemperature = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Populate RadioButtonList with proper values
                string[] arr = new string[] { "F", "C" };

                ListItem[] myList = new ListItem[arr.Length];

                for (int i = 0; i < myList.Length; i++)
                {
                    //ListItem with text and value properties
                    myList[i] = new ListItem(arr[i], arr[i]);
                }

                RadioButtonList1.Items.AddRange(myList);
                RadioButtonList1.AutoPostBack = true;
            }
            else
            {
                ViewState["temperature"] = RadioButtonList1.SelectedValue;

                //Read another value stored in View state
                if (ViewState["Person"] != null)
                {
                    Person p = (Person)ViewState["Person"];
                    System.Diagnostics.Debug.WriteLine("First name is ", p.FirstName);
                }
            }
            
            if (ViewState["temperature"] != null)
            {
                storedTemperature = (string)ViewState["temperature"];
                System.Diagnostics.Debug.WriteLine("storedTemperature {0}", storedTemperature);
                Label1.Text = storedTemperature;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("storedTemperature in PreRender", storedTemperature);
            Person p = new Person("Walt", "Kowalsky");
            ViewState["Person"] = p;
        }

    }
}